﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SMS.DAL.Providers
{
    public class ItemProvider : ProviderBase<Item, ItemSearchCriteria>
    {
        private Item item;

        public ItemProvider()
        {
            item = new Item();
        }

        public override void Insert(Item item)
        {
            Context.Items.AddObject(item);
            Context.SaveChanges();
        }

        public int InsertItem(Item item)
        {
            Context.Items.AddObject(item);
            Context.SaveChanges();
            return item.ID;
        }

        public Item GetItemByBarCode(string barCode)
        {
            return (from i in Context.Items where (i.BarCode == barCode)
                select i).FirstOrDefault();
        }

        public Item GetByPartNumber(string PartNumber)
        {
            return (from i in Context.Items
                    where i.PartNumber == PartNumber
                    select i).FirstOrDefault();
        }

        public override void Update(Item updatedItem)
        {
            Item oldItem = (from i in Context.Items
                            where (updatedItem.ID == i.ID)
                            select i).FirstOrDefault();

            oldItem.BarCode = updatedItem.BarCode;
            oldItem.ManfCountry = updatedItem.ManfCountry;
            oldItem.Mark = updatedItem.Mark;
            oldItem.Name = updatedItem.Name;
            oldItem.SellingPrice = updatedItem.SellingPrice;
            oldItem.PartNumber = updatedItem.PartNumber;

            Context.SaveChanges();
        }

        public override void Delete(int id)
        {
            throw new NotImplementedException();
        }

        public override Item GetByID(int id)
        {
            return (from i in Context.Items
                    where i.ID == id
                    select i).FirstOrDefault();
        }

        public override List<Item> Search(ItemSearchCriteria SearchCriteria)
        {
            return (from i in Context.Items
                    where ((i.BarCode == SearchCriteria.Barcode) ||
                    (i.PartNumber == SearchCriteria.PartNumber) ||
                    ((i.Name == SearchCriteria.Name) && (i.Mark == SearchCriteria.Mark)) ||
                    (i.PartNumber == SearchCriteria.PartNumber))

                    select i).ToList<Item>();
        }

        public List<Item> UserSearch(ItemSearchCriteria itemSearch)
        {
            var quary = (from s in Context.Items.Include("StockItems") 
                            where (((itemSearch.Barcode == s.BarCode) || (itemSearch.Barcode == string.Empty)) &&
                            ((itemSearch.PartNumber == s.PartNumber) || (itemSearch.PartNumber == string.Empty))&&
                            ((s.Mark.Contains(itemSearch.Mark)) || (itemSearch.Mark == string.Empty)) &&
                            ((s.Name.Contains(itemSearch.Name)) || (itemSearch.Name == string.Empty)))
                        select s).ToList();
            return quary;
        }

        public List<Item> GetAll()
        {
            return (from i in Context.Items
                    select i).ToList();
        }
    }
}